package com.example.demo_job.controller;


import com.example.demo_job.entity.Position;
import com.example.demo_job.entity.PositionRescruit;
import com.example.demo_job.service.PositionService;
import com.example.demo_job.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/position")
//@CrossOrigin(origins = {"*"},methods = {RequestMethod.GET, RequestMethod.POST})
//@CrossOrigin
public class PositionController {
    @Autowired
    private PositionService positionService;

    //展示岗位地址：http://localhost:8080/hqdx/position/ShowPosition
    @RequestMapping("/ShowPosition")
    public R ShowPosition(String cusername){
        List<PositionRescruit> positionRescruitList=positionService.ShowPosition(cusername);
        return R.ok().data("items",positionRescruitList);
    }


//    搜索岗位地址：http://localhost:8080/hqdx/position/searchPosition
    @RequestMapping("/searchPosition")
    public R searchPosition(int cid, String value){
        value="%"+value+"%";
        List<PositionRescruit> positionRescruitList=positionService.searchPosition(cid,value);
        return R.ok().data("items",positionRescruitList);
    }


    //删除岗位地址：http://localhost:8080/hqdx/position/DeletePosition
    @RequestMapping("/DeletePosition")
    public R DeletePosition(int pid){
        String f="src\\main\\resources\\static\\PositionTxt\\"+positionService.FindInfo(pid);
        File file =new File(f);
        file.delete();
        try {
            positionService.DeletePosition(pid);
        }catch (Exception e) {
            return R.error();
        }
        return R.ok().message("删除成功");
    }


    //添加岗位地址：http://localhost:8080/hqdx/position/InsertPosition
    @RequestMapping("/InsertPosition")
    public R InsertPosition(String cid,String pname,String salary,String paddress,String enumber,String cv){
        String name=pname;
        double aDouble = Double.parseDouble(salary);
        int enumber2 = Integer.valueOf(enumber);
        int cid1=Integer.valueOf(cid);
        String pinfo=cid+name+".txt";
        File txtfile=new File("src\\main\\resources\\static\\PositionTxt\\"+pinfo);
        try {
            txtfile.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            positionService.InsertPosition(cid1, pname, pinfo, aDouble ,paddress, enumber2);
            int pid=positionService.findPidByPnameAndCid(cid1,pname);
            revisePCV(pid,cv);
        }catch (Exception e){
            return R.error().message("此岗位已存在");
        }
        return R.ok().message("岗位添加成功");
    }


    //更改岗位信息渲染地址：http://localhost:8080/hqdx/position/findpositionBypid
    @RequestMapping("/findpositionBypid")
    public R FindPositionBypid(int pid){
        Position position = positionService.FindPositionBypid(pid);
        return R.ok().data("items",position);
    }



    //更改岗位信息地址：http://localhost:8080/hqdx/position/UpdatePosition
    @RequestMapping("/UpdatePosition")
    public R UpdatePosition(String paddress,int pid,String pname,String salary,String enumber,String cv){
        if(pname.equals(positionService.findPnameBypid(pid))){
            double salary1=Double.parseDouble(salary);
            int enumber1 =Integer.valueOf(enumber);
            positionService.UpdatePosition(pid, pname, salary1, paddress, enumber1);
            revisePCV(pid,cv);
            return R.ok().message("更新成功");
        }
        return R.error().message("岗位名字不可修改");
    }



    //无需与前端相连
    @RequestMapping("/revisePCV")
    public R revisePCV(int pid,String cv){
        String Tsinfo= positionService.FindPinfo(pid);
        if(Tsinfo==null) return R.error().message("失败");
        positionService.Write(Tsinfo,cv);
        return R.ok().message("修改成功");
    }

}
